package com.jf.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sys.android.util.DialogFactory;
import com.sys.android.util.FinalUntil;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

@ContentView(R.layout.oftenaddress)
public class OftenAddress extends Activity {

	private ListView datalist = null; // 定义ListView组件
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>(); // 定义显示的内容包装
	private SimpleAdapter simpleAdapter = null; // 进行数据的转换操作

	@ViewInject(R.id.personName)
	private EditText personName;

	@ViewInject(R.id.personPhone)
	private EditText personPhone;

	@ViewInject(R.id.personAddres)
	private EditText personAddres;

	@ViewInject(R.id.button2)
	private Button button2;

	private SharedPreferences useShare;

	private String userId;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);// xml视图加载的处理

		useShare = getSharedPreferences("userInfo", MODE_PRIVATE);
		userId = useShare.getString("username", "");

		this.datalist = (ListView) super.findViewById(R.id.listView1); // 取得组件

		RequestParams params = new RequestParams();
		params.addQueryStringParameter("mobileUserId", userId);

		HttpUtils http = new HttpUtils();

		http.send(HttpMethod.POST,
				FinalUntil.ServerAddress+"findMoblieAddress", params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("RegisterUser", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// arg0.result返回服务器结果

						Log.i("RegisterUser", arg0.result);
						try {
							JSONArray jsonArr = new JSONArray(arg0.result);
							for (int i = 0; i < jsonArr.length(); i++) {
								JSONObject json = jsonArr.getJSONObject(i);
								Map<String, String> map = new HashMap<String, String>(); // 定义Map集合，保存每一行数据
								map.put("sendName", json.getString("sendName")); // 与data_list.xml中的TextView组加匹配
								map.put("sendcall", json.getString("sendTel")); // 与data_list.xml中的TextView组加匹配
								map.put("sendaddress",
										json.getString("sendAddress")); // 与data_list.xml中的TextView组加匹配
								list.add(map); // 保存了所有的数据行
							}
							simpleAdapter = new SimpleAdapter(
									OftenAddress.this, list,
									R.layout.my_sendfilelist, new String[] {
											"sendName", "sendcall",
											"sendaddress" } // Map中的key的名称
									, new int[] { R.id.sendName, R.id.sendcall,
											R.id.sendaddress }); // 是data_list.xml中定义的组件的资源ID
							datalist.setAdapter(simpleAdapter);
							datalist.setOnItemClickListener(new OnItemClickListenerImpl()); // 单击选项
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				});
	}

	public void finishAddress(View view) {
		this.finish();
	}

	private class OnItemClickListenerImpl implements OnItemClickListener {

		@SuppressWarnings("unchecked")
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Map<String, String> map = (Map<String, String>) OftenAddress.this.simpleAdapter
					.getItem(position);
			String sendName = map.get("sendName");
			String sendcall = map.get("sendcall");
			String sendaddress = map.get("sendaddress");
			// Toast.makeText(getApplicationContext(), data[position][0], 0)
			// .show();
		}
	}

	public void AddInfoAdress(View v) {
		final Dialog dialog = DialogFactory.creatRequestDialog(OftenAddress.this,
				"数据传输中……");
		dialog.show();
		RequestParams params = new RequestParams();

		params.addQueryStringParameter("sendName", personName.getText()
				.toString());
		params.addQueryStringParameter("sendTel", personPhone.getText()
				.toString());
		params.addQueryStringParameter("sendAddress", personAddres.getText()
				.toString());
		params.addQueryStringParameter("sendItems", "");

		params.addQueryStringParameter("userId", userId);

		HttpUtils http = new HttpUtils();

		http.send(HttpMethod.POST,
				FinalUntil.ServerAddress+"saveMoblieAddress", params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("RegisterUser", arg1);
						Toast.makeText(getApplicationContext(), "网络请求异常", 0)
						.show();
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// arg0.result返回服务器结果
						Log.i("RegisterUser", arg0.result);
						Map<String, String> map = new HashMap<String, String>(); // 定义Map集合，保存每一行数据
						map.put("sendName", personName.getText().toString());
						map.put("sendcall", personPhone.getText().toString());
						map.put("sendaddress", personAddres.getText()
								.toString());
					
						list.add(map); // 保存了所有的数据行
						simpleAdapter.notifyDataSetChanged();
						try {
							JSONObject json = new JSONObject(arg0.result);
							Toast.makeText(getApplicationContext(), json.getString("result"), 0)
							.show();
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						personName.setText("");
						personPhone.setText("");
						personAddres.setText("");
						dialog.hide();
					}
				});

	}
}
